import 'package:enjoy_plus_hm/pages/house/addHouse.dart';
import 'package:enjoy_plus_hm/pages/house/components/buiding_list.dart';
import 'package:enjoy_plus_hm/pages/house/components/house_form.dart';
import 'package:enjoy_plus_hm/pages/house/components/room_list.dart';
import 'package:enjoy_plus_hm/pages/house/house_detail.dart';
import 'package:enjoy_plus_hm/pages/house/index.dart';
import 'package:enjoy_plus_hm/pages/home/detail.dart';
import 'package:enjoy_plus_hm/pages/login/index.dart';
import 'package:enjoy_plus_hm/pages/profile/index.dart';
import 'package:enjoy_plus_hm/pages/stores/counter.dart';
import 'package:enjoy_plus_hm/pages/tab_bar_page.dart';
import 'package:enjoy_plus_hm/utils/token.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

void main() {
  runApp(
    MultiProvider(
      providers: [ChangeNotifierProvider(create: (ctx) => CounterModerl())],
      child: MaterialApp(
        routes: {
          '/': (context) => const TabBarPage(),
          //详情页路由
          '/detail': (context) => const NoticeDetail(),
          //登录页
          '/login': (context) => const LoginPage(),
        },
        initialRoute: '/',
        //路由拦截
        /*
      1.判断有没有token
      2.如果没有token,再判断是否前往的是登录页
      3.如果是登录页,则直接进登陆页
      4.如果不是登录页,则需要跳转到登录页
      5.如果有token,则直接放行
      */
        onGenerateRoute: (settings) {
          print(settings);
          //先获取token
          String? token = TokenManager().getToken() ?? '';
          print(token);
          //判断是否存在token,或者是不是前往登录页
          if (token.isEmpty && settings.name != '/login') {
            return MaterialPageRoute(builder: (context) => const LoginPage());
          }

          //个人信息页
          if (settings.name == '/profile') {
            print(settings.arguments);
            return MaterialPageRoute(
              builder: (context) =>
                  ProfilePage(userInfo: settings.arguments as Map),
              // settings: settings,
            );
          }

          //房屋列表页
          if (settings.name == '/house_list') {
            return MaterialPageRoute(
              builder: (context) => const HouseList(),
              settings: settings,
            );
          }

          //添加房屋页面
          if (settings.name == '/add_house') {
            return MaterialPageRoute(
              builder: (context) => const AddHouse(),
              // settings: settings,
            );
          }

          //选择楼栋页面
          if (settings.name == '/building_list') {
            return MaterialPageRoute(
              builder: (context) =>
                  BuildingList(point: (settings.arguments as Map)['point']),
              // settings: settings,
            );
          }

          //选择房间号页面
          if (settings.name == '/room_list') {
            final temp = settings.arguments as Map;
            return MaterialPageRoute(
              builder: (context) =>
                  RoomList(point: temp['point'], building: temp['building']),
              // settings: settings,
            );
          }

          //添加房屋信息页面
          if (settings.name == '/house_form') {
            final temp = settings.arguments as Map;
            print(temp);
            return MaterialPageRoute(
              builder: (context) => HouseForm(
                point: temp['point'],
                building: temp['building'],
                room: temp['room'],
                editForm: temp['editForm'],
              ),
              // settings: settings,
            );
          }

          //房屋详情页
          if (settings.name == '/house_detail') {
            // print(settings.arguments);
            return MaterialPageRoute(
              builder: (context) =>
                  HouseDetail(houseId: (settings.arguments as Map)['houseId']),
              // settings: settings,
            );
          }

          //404页面
          return MaterialPageRoute(
            builder: (context) {
              return const TabBarPage();
            },
          );
        },
      ),
    ),
  );
}
